//
//  BaseNavController.m
//  pmmSS
//
//  Created by Mac book on 2017/8/25.
//  Copyright © 2017年 WTFSSD. All rights reserved.
//

#import "BaseNavController.h"

@interface BaseNavController ()<UIGestureRecognizerDelegate>

@property(nonatomic,assign)UIStatusBarStyle statusBarStyle;

@end

@implementation BaseNavController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.interactivePopGestureRecognizer.delegate = self;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeStatusNoti:) name:ChangeStatusBarStyleNotificationKey object:nil];
    self.statusBarStyle = UIStatusBarStyleLightContent;

}

-(void)changeStatusNoti:(NSNotification*)noti{
    if ([noti.name isEqualToString:ChangeStatusBarStyleNotificationKey]) {
        UIStatusBarStyle tyyle = [noti.userInfo[@"StatusBarStyle"] integerValue];
        self.statusBarStyle = tyyle;
        [self setNeedsStatusBarAppearanceUpdate];

    }
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:ChangeStatusBarStyleNotificationKey object:nil];
    NSLog(@"导航控制器销毁了");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.childViewControllers.count > 0) {
        UIButton * backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
        [backButton addTarget:self action:@selector(backButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [backButton setImage:[UIImage imageNamed:@"icon_nav_arrow_left"] forState:UIControlStateNormal];
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    }
    if (self.childViewControllers.count==1) {
        viewController.hidesBottomBarWhenPushed = YES; //viewController是将要被push的控制器
    }
    [super pushViewController:viewController animated:animated];
}

-(void)backButtonClick:(id)sender{
    if (self.backDelegate&&[self.backDelegate respondsToSelector:@selector(BaseNavgationControllerCanBack:)]) {
        if([self.backDelegate BaseNavgationControllerCanBack:self]){
            [self popViewControllerAnimated:true];
        }else{
            if ([self.backDelegate respondsToSelector:@selector(BaseNavgationController:didClickBackButton:)]) {
                [self.backDelegate BaseNavgationController:self didClickBackButton:sender];
            }
        }
    }else{
        [self popViewControllerAnimated:true];
    }
}

-(UIViewController *)popViewControllerAnimated:(BOOL)animated{
    return [super popViewControllerAnimated:animated];
}


- (UIStatusBarStyle)preferredStatusBarStyle{
    return self.statusBarStyle;
}
//禁止屏幕旋转
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

{

    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);

}
-(BOOL)shouldAutorotate{
    return NO;
}
//支持的方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}


@end
